package com.chc.web.dash_board.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.chc.web.dash_board.entity.vo.DashboardVo;
import com.chc.web.dash_board.service.DashBoardService;
import com.chc.web.fee_park.entity.FeePark;
import com.chc.web.fee_park.service.FeeParkService;
import com.chc.web.fee_power.entity.FeePower;
import com.chc.web.fee_power.service.FeePowerService;
import com.chc.web.fee_water.entity.FeeWater;
import com.chc.web.fee_water.service.FeeWaterService;
import com.chc.web.live_user.service.LiveUserService;
import com.chc.web.park_list.service.ParkListService;
import com.chc.web.user_complaint.service.UserComplaintService;
import com.chc.web.user_repair.service.UserRepairService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author: chc
 * @Date: 2024/11/10/22:10
 * @Description:
 */
@Service
@Slf4j
public class DashBoardServiceImpl implements DashBoardService {

    @Autowired
    private LiveUserService liveUserService;
    @Autowired
    private UserComplaintService userComplaintService;
    @Autowired
    private UserRepairService userRepairService;
    @Autowired
    private ParkListService parkListService;
    @Autowired
    private FeeParkService feeParkService;
    @Autowired
    private FeePowerService feePowerService;
    @Autowired
    private FeeWaterService feeWaterService;


    /**
     * 传给前端的可视化数据
     * @param format
     * @return
     */
    @Override
    public DashboardVo getInfoCount(String format) {
        try {
            Date[] dates = getTodayDate();
            Date beginTime = dates[0];
            Date endTime = dates[1];
            DashboardVo dashboardVo = new DashboardVo();
            //近七个月停车费
            dashboardVo.setSevenMonthParkConsumptionList(feeParkService.selectSevenMonthConsumptionList());
            //近七个月电费
            dashboardVo.setSevenMonthPowerConsumptionList(feePowerService.selectSevenMonthPowerConsumptionList());
            dashboardVo.setTodayToRoomNum(liveUserService.getBaseMapper().selectCount(null));
            dashboardVo.setRepairNum(userRepairService.getBaseMapper().selectCount(null));
            dashboardVo.setCatNumber(parkListService.getBaseMapper().selectCount(null));
            dashboardVo.setTodayComplainNum(userComplaintService.getBaseMapper().selectCount(null));
            dashboardVo.setConsumptionNum(selectFeeCountPrice().longValue());
            return dashboardVo;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 查询总费用
     * @return
     */
    private Double selectFeeCountPrice() {
        LambdaQueryWrapper<FeePark> parkWrapper = Wrappers.lambdaQuery();
        //获取当天的时间
        String NowDayTime = new SimpleDateFormat("yyyy-MM").format(new Date());
        parkWrapper.eq(FeePark::getPayParkMonth, NowDayTime);
        List<FeePark> parkList = feeParkService.getBaseMapper().selectList(parkWrapper);

        LambdaQueryWrapper<FeePower> powerWrapper = Wrappers.lambdaQuery();
        powerWrapper.eq(FeePower::getPayPowerMonth, NowDayTime);
        List<FeePower> powerList = feePowerService.getBaseMapper().selectList(powerWrapper);

        LambdaQueryWrapper<FeeWater> waterWrapper = Wrappers.lambdaQuery();
        waterWrapper.eq(FeeWater::getPayWaterMonth, NowDayTime);
        List<FeeWater> waterList = feeWaterService.getBaseMapper().selectList(waterWrapper);

        Double totalFee = 0.0;
        for (FeePark park : parkList) {
            totalFee += Double.parseDouble(String.valueOf(park.getPayParkMoney()));
        }
        for (FeePower power : powerList) {
            totalFee += Double.parseDouble(String.valueOf(power.getPayPowerMoney()));
        }
        for (FeeWater water : waterList) {
            totalFee += Double.parseDouble(String.valueOf(water.getPayWaterMoney()));
        }

        return totalFee;
    }

    /**
     * 获取一天时间
     *
     * @return date[0]开始时间, date[1]结束时间
     * @throws ParseException
     */
    private Date[] getTodayDate() throws ParseException {
        Date today = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        String date = sf.format(today);
        String beginDate = date + " 00:00:00";
        String endDate = date + " 23:59:59";
        SimpleDateFormat timeSf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return new Date[]{timeSf.parse(beginDate), timeSf.parse(endDate)};
    }

}
